package com.lxj.security.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.lxj.security.common.api.CommonResult;
import com.lxj.security.entity.LoginParam;
import com.lxj.security.service.UserService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@RestController
@RequestMapping("/user")
public class UserController {
    @Value("${jwt.tokenHead}")
    private String tokenHead;
    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public CommonResult login(@RequestBody LoginParam loginParams) {

        HashMap<String, String> data = new HashMap<>();
        String token = null;
        try {
            token = userService.login(loginParams);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.validateFailed("用户名或密码错误");
        }
        if (StringUtils.isEmpty(token)) {
            return CommonResult.validateFailed("用户名或密码错误");
        }
        data.put("tokenHead", tokenHead);
        data.put("access_token", token);
        // localStorage.setItem("Authorization","Bearer sdsdfdfds")
        // $ajax{data:{},type:"",header:{"Authorization":"Bearer sdsdfdfds"}}
        return CommonResult.success(data);
    }
    @PostMapping()
    public  String hello(){
        return "welcome";
    }
}
